const app = getApp()
import {
        getInfo,
} from '../../../api/member'

import {
        update
} from '../../../api/user'

Page({
        data: {
                avatar:"",
                haveAvatar:false,
                nickname: "",
                company: "",
                mobile: "",
                gender: false,
                birthday: false,
                children: false,
                Invitationcod:"",
                promoterName:""
        },

        /**
         * 生命周期函数--监听页面加载
         */
        onLoad: function(options) {
                console.info("接收到的参数" + JSON.stringify(options))
                this.getInfo()
        },

        /**
         * 生命周期函数--监听页面初次渲染完成
         */
        onReady: function() {

        },

        /**
         * 生命周期函数--监听页面显示
         */
        onShow: function() {
                this.checkAuthorize()
        },

        getInfo(){
                getInfo().then(
                        res => {
                                console.log("接收用户信息--" + JSON.stringify(res))
                                if (res.type == 'success') {
                                        this.data.promoterName=res.data.promoterName
                                        this.data.Invitationcod=Number(res.data.member.id)+10200
                                        console.log(this.data.Invitationcod);
                                        let mobile = "" + res.data.member.mobile
                                        mobile = mobile.substr(0, 3) + "****" + mobile.substr(7, 10)
                                        this.setData({
                                                company: res.data.member.company,
                                                mobile: mobile,
                                                // gender: res.data.member.gender ,
                                                birthday: res.data.member.birth,
                                                Invitationcod:this.data.Invitationcod,
                                                promoterName:this.data.promoterName
                                        })
                                }
                        }
                ).catch(error => {
                        console.log(error)
                })
        },

        // 检查头像的授权状态
        checkAuthorize() {
                console.info("我进检查了")
                let userInfo = app.globalData.userInfo
                console.info("userInfo--" + JSON.stringify(userInfo))
                if (userInfo.hasOwnProperty("nickname")){
                     this.setData({
                             nickname: app.globalData.userInfo.nickname
                     })  
                }
                if (userInfo.hasOwnProperty("avatar")){
                     this.setData({
                             haveAvatar: true,
                             avatar: app.globalData.userInfo.avatar,
                     })  
                }
        },

        getUserInfo: function (e) {
                console.log("用户信息授权的数据e--"+JSON.stringify(e))
                let sendData = {}
                sendData.avatar = e.detail.userInfo.avatarUrl
                sendData.nickname = e.detail.userInfo.nickName
                
                update(sendData).then(
                        res => {
                                console.log("接收用户信息--" + JSON.stringify(res))
                                if (res.type == 'success') {
                                        this.setData({
                                                haveAvatar: true,
                                                avatar: res.data.avatar,
                                                nickname: res.data.nickname,
                                        })
                                }
                        }
                ).catch(error => {
                        console.log(error)
                })
        },

        //密码设置页面
        toSetPassword: function(e) {
                console.info("准备跳转密码设置页面")
        },

        //清理缓存
        clearnStore: function(e) {
                console.info("缓存被清理了")
        },

        //退出账号
        loginOut: function(e) {
                console.info("我被点击了")
                wx.navigateTo({
                        url: '/pages/login/msgLogin',
                })
        }
})